import service from "@/api/service";
export default {
  namespaced: true,
  state: {
    treeData: [],
    currentItem: {
      id: null,
      children: [],
      data:{
        appId: null,
        menuId: null,
        parentMenuId: null,
        menuName: null,
        menuPath: null,
        label: null,
        icon: null,
        ordinal: null
      }
    },
  },
  mutations: {
    setData(state, treeData) {
      state.treeData = treeData;
    },
    selectItem(state, item) {
      state.currentItem = item;
    },
    initItem(state){
      state.currentItem = {
        id: null,
        children: [],
        data:{
          appId: null,
          menuId: null,
          parentMenuId: null,
          menuName: null,
          menuPath: null,
          label: null,
          icon: null,
          ordinal: null
        }
      };
    }
  },
  actions: {
    query({ commit, state }) {
      return service.menu
        .query()
        .then((response) => {
          commit("setData", response.success ? response.data : null);
          return response;
        })
        .catch((error) => {
          return error;
        });
    },
    create({ commit, state, dispatch }, dto) {
      return service.menu
        .create({ data: dto })
        .then((response) => {
          if (response.success) dispatch("query");
          return response;
        })
        .catch((error) => {
          return error;
        });
    },
    update({ commit, state, dispatch }, dto) {
      return service.menu
        .update({ data: dto })
        .then((response) => {
          if (response.success) dispatch("query");
          return response;
        })
        .catch((error) => {
          return error;
        });
    },
    delete({ commit, state, dispatch }, path) {
      return service.menu
        .delete({ data: path })
        .then((response) => {
          if (response.success) dispatch("query");
          return response;
        })
        .catch((error) => {
          return error;
        });
    }
    // modules({ commit, state }, appId, menuId) {
    //   return service.menu
    //     .modules({ params: { appId, menuId } })
    //     .then((response) => {
    //       return response;
    //     })
    //     .catch((error) => {
    //       return error;
    //     });
    // },
  },
};
